package com.imddy.tweb.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleDateUtils {

	private static Log log = LogFactory.getLog(SimpleDateUtils.class);
	
	public static final String FORMAT_STANDARDSTR_YYYYMMDD = "yyyy-MM-dd";
	public static final String FORMAT_STANDARDSTR_HHMMSS = "HH:mm:ss";
	public static final String FORMAT_STANDARDSTR_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_STANDARDSTR_YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String FORMAT_STDANDARDSTR_YYYYMM = "yyyy-MM";
	public static final String FORMAT_STDANDARDSTR_MMDD = "MM-dd";
	public static final String FORMAT_STDANDARDSTR_HHMM = "HH:mm";
	public static final String FORMAT_STDANDARDSTR_MMSS = "mm:ss";
	
	public static final String FORMAT_NOSPLITSTR_YYYYMMDD = "yyyyMMdd";
	public static final String FORMAT_NOSPLITSTR_HHMMSS = "HHmmss";
	public static final String FORMAT_NOSPLITSTR_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String FORMAT_NOSPLITSTR_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
	public static final String FORMAT_NOSPLITSTR_YYYYMM = "yyyyMM";
	public static final String FORMAT_NOSPLITSTR_MMDD = "MMdd";
	public static final String FORMAT_NOSPLITSTR_HHMM = "HHmm";
	public static final String FORMAT_NOSPLITSTR_MMSS = "mmss";
	
    public static final String DAY_BEGIN_STRING_HHMMSS = "00:00:00";
    public static final String DAY_END_STRING_HHMMSS = "23:59:59";
	
	public static final String YEAR_FIELD = "YEAR";
	public static final String MONTH_FIELD = "MONTH";
	public static final String DAY_FIELD = "DAY";
	public static final String HOUR_FIELD = "HOUR";
	public static final String MINUTE_FIELD = "MINUTE";
	public static final String SECOND_FIELD = "SECOND";
	public static final String MILLISECOND_FIELD = "MILLISECOND";
	
	/**
	 * 根据传入的时间字符串和解析时间格式字符串返回生成Date类型
	 * @param dateStr
	 * @param formatStr
	 * @return
	 */
	public static Date parseDate(String dateStr, String formatStr) {
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
		try {
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 根据传入的Date和格式化时间字符串返回格式化后的时间字符串
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String formatDate(Date date, String formatStr) {
		String dateStr = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
		try {
			dateStr = simpleDateFormat.format(date);
			return dateStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}
	
	
	/**
	 * 时间加减方法，原时间date, 加上的数值， 加上数值的类型，返回加减后的时间
	 * @param date
	 * @param amounts
	 * @param type
	 * @return
	 */
	public static Date addDate(Date date, int amounts, String type) {
		Date resultDate = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int intType = 0;
		switch (type) {
		case "YEAR":
			intType = Calendar.YEAR;
			break;
		case "MONTH":
			intType = Calendar.MONTH;
			break;
		case "DAY":
			intType = Calendar.DAY_OF_MONTH;
			break;
		case "HOUR":
			intType = Calendar.HOUR_OF_DAY;
			break;
		case "MINUTE":
			intType = Calendar.MINUTE;
			break;
		case "SECOND":
			intType = Calendar.SECOND;
			break;
		case "MILLISECOND":
			intType = Calendar.MILLISECOND;
			break;
		default:
			log.error("传入日期格式出错");
			break;
		}
		if (intType == 0) {
			log.error("传入日期格式出错");
		} else {
			calendar.add(intType, amounts);
		}
		resultDate = calendar.getTime();
		return resultDate;
	}
	
	
	/**
	 * 根据传入的Date类型来获取Calendar
	 * @param date
	 * @return
	 */
	public static Calendar getCalendarOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
	
	/**
	 * 根据传入的字符串来获取Calendar
	 * @param year
	 * @param month ,month在Calendar是从0开始，我们这里从1开始，没有0月。
	 * @param day
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Calendar getCalendarOfString(String year, String month, String day, String hour, String minute, String second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.valueOf(year), Integer.valueOf(month) - Integer.valueOf(1), Integer.valueOf(day), Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second) );
		return calendar;
	}
	
	/**
	 * 根据传入的Integer来获取Calendar
	 * @param year
	 * @param month ,month在Calendar是从0开始，我们这里从1开始，没有0月。
	 * @param day
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Calendar getCalendarOfInteger(int year, int month, int day, int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.valueOf(year), Integer.valueOf(month) - Integer.valueOf(1), Integer.valueOf(day), Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second) );
		return calendar;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Date date = SimpleDateUtils.parseDate("20171209", SimpleDateUtils.FORMAT_NOSPLITSTR_YYYYMMDD);
		System.out.println(SimpleDateUtils.formatDate(date, SimpleDateUtils.FORMAT_STANDARDSTR_YYYYMMDD));
		
		Date date2 = SimpleDateUtils.parseDate("20160408", SimpleDateUtils.FORMAT_NOSPLITSTR_YYYYMMDD);
		Date date3 = SimpleDateUtils.addDate(date2, -28, SimpleDateUtils.MONTH_FIELD);
		System.out.println(SimpleDateUtils.formatDate(date3, SimpleDateUtils.FORMAT_STANDARDSTR_YYYYMMDD));
		
		Calendar calendar = SimpleDateUtils.getCalendarOfString("2017", "1", "5", "23", "12", "21");
		
		
		System.out.println(SimpleDateUtils.getCalendarOfDate(date).get(Calendar.YEAR));
		System.out.println(SimpleDateUtils.getCalendarOfDate(date).get(Calendar.MONTH));
		System.out.println(SimpleDateUtils.getCalendarOfDate(date).get(Calendar.DAY_OF_MONTH));
		
		System.out.println(calendar.getTime());
		System.out.println(calendar.get(Calendar.MONTH));
		

	}

}
